package com.zywl.api;

import com.zywl.core.beans.Dictionary;
import com.zywl.service.DictionaryService;
import com.zywl.utils.models.JsonResult;
import com.zywl.web.controller.base.BaseController;
import com.zywl.web.controller.form.DictForm;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/dictionary/")
public class DictionaryApi extends BaseController {
    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 获取行业
     * @return
     */
    @ResponseBody
    @RequestMapping("findIndustrys")
    public JsonResult findIndustrys(){
        JsonResult jr = new JsonResult();
        try {
            List<Dictionary> dictionaryList = dictionaryService.getDictionaryByType("indu");
            List<DictForm> dictFormList = new ArrayList<>();
            boolean fage = false;
            for (Dictionary dictionary : dictionaryList){
                if(StringUtils.isEmpty(dictionary.getDisParentCode())){
                    DictForm dictForm = new DictForm();
                    BeanUtils.copyProperties(dictionary, dictForm);
                    dictFormList.add(dictForm);
                }else{
                    fage = true;
                }
            }
            if(fage){
                for (DictForm form : dictFormList){
                    form.setDictionaryList(getChild(form.getDisCode(), dictionaryList));
                }
            }
            jr.success("操作成功",dictFormList);
        }catch (Exception e){
            logger.error("操作失败",e);
            jr.error("操作失败");
        }
        return jr;
    }

    /**
     * 获取标签
     * @return
     */
    @ResponseBody
    @RequestMapping("findTags")
    public JsonResult findTags(){
        JsonResult jr = new JsonResult();
        try {
            List<Dictionary> dictionaryList = dictionaryService.getDictionaryByType("tags");
            boolean fage = false;
            List<DictForm> dictFormList = new ArrayList<>();
            for (Dictionary dictionary : dictionaryList){
                if(StringUtils.isEmpty(dictionary.getDisParentCode())){
                    DictForm dictForm = new DictForm();
                    BeanUtils.copyProperties(dictionary, dictForm);
                    dictFormList.add(dictForm);
                }else{
                    fage = true;
                }
            }
            if(fage){
                for (DictForm form : dictFormList){
                    form.setDictionaryList(getChild(form.getDisCode(), dictionaryList));
                }
            }

            jr.success("操作成功",dictFormList);
        }catch (Exception e){
            logger.error("操作失败",e);
            jr.error("操作失败");
        }
        return jr;
    }

    /**
     * 子方法
     **/
    private List<DictForm> getChild(String id, List<Dictionary> rootMenu) {
        // 子菜单
        List<DictForm> childList = new ArrayList<>();

        for (Dictionary dictionary : rootMenu) {
            DictForm dictForm = new DictForm();
            BeanUtils.copyProperties(dictionary, dictForm);
            // 遍历所有节点，将父菜单id与传过来的id比较
            if (StringUtils.isNotEmpty(dictionary.getDisParentCode())) {
                if (id.equals(dictionary.getDisParentCode())) {
                    childList.add(dictForm);
                }
            }
        }
        // 把子菜单的子菜单再循环一遍
        for (DictForm dictForm : childList) {
            dictForm.setDictionaryList(getChild(dictForm.getDisCode(), rootMenu));
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }
}
